package com.htc.model.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.constants.Constants;
import com.htc.model.dto.InstructorDTO;

public class InstructorModel {
	public static InstructorModel instance;
	public static InstructorModel getInstance(){
		if(instance == null)
			instance = new InstructorModel();
		return instance;
	}
	
	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT instructor_id FROM " + Constants.TBL_INSTRUCTOR
				+ " where instructor_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertInstruc(InstructorDTO ins, Context context) {
		boolean flag = false;
		if (!checkDB(context, ins.getInstrutorId())) {
			String DATABASE_TABLE = Constants.TBL_INSTRUCTOR;
			ContentValues values = new ContentValues();
			values.put("instructor_id", ins.getInstrutorId());
			values.put("instructor_name", ins.getInstructorName());
			values.put("instructor_name_ar", ins.getInstructorNameAr());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateInstruc(InstructorDTO ins, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_INSTRUCTOR;
		String where = "instructor_id='" + ins.getInstrutorId() + "'";
		ContentValues values = new ContentValues();
		
		values.put("instructor_name", ins.getInstructorName());
		values.put("instructor_name_ar", ins.getInstructorNameAr());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteInstruc(InstructorDTO ins, Context context) {
		String sql = "instructor_id='" + ins.getInstrutorId() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_INSTRUCTOR;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
